package cn.mst.vrs.service.payrefund;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.mst.vrs.controller.admin.payrefund.vo.PayRefundPageReqVO;
import cn.mst.vrs.controller.admin.payrefund.vo.PayRefundSaveReqVO;
import cn.mst.vrs.dal.dataobject.payrefund.PayRefundDO;
import cn.mst.vrs.dal.mysql.payrefund.PayRefundMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mst.vrs.enums.ErrorCodeConstants.PAY_REFUND_NOT_EXISTS;

/**
 * 支付退款单 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PayRefundServiceImpl implements PayRefundService {

    @Resource
    private PayRefundMapper payRefundMapper;

    @Override
    public Long createPayRefund(PayRefundSaveReqVO createReqVO) {
        // 插入
        PayRefundDO payRefund = BeanUtils.toBean(createReqVO, PayRefundDO.class);
        payRefundMapper.insert(payRefund);
        // 返回
        return payRefund.getId();
    }

    @Override
    public void updatePayRefund(PayRefundSaveReqVO updateReqVO) {
        // 校验存在
        validatePayRefundExists(updateReqVO.getId());
        // 更新
        PayRefundDO updateObj = BeanUtils.toBean(updateReqVO, PayRefundDO.class);
        payRefundMapper.updateById(updateObj);
    }

    @Override
    public void deletePayRefund(Long id) {
        // 校验存在
        validatePayRefundExists(id);
        // 删除
        payRefundMapper.deleteById(id);
    }

    private void validatePayRefundExists(Long id) {
        if (payRefundMapper.selectById(id) == null) {
            throw exception(PAY_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public PayRefundDO getPayRefund(Long id) {
        return payRefundMapper.selectById(id);
    }

    @Override
    public PageResult<PayRefundDO> getPayRefundPage(PayRefundPageReqVO pageReqVO) {
        return payRefundMapper.selectPage(pageReqVO);
    }

}